#include "parser1.h"

void mongetc(FILE*f,char*src)
{
  int l,i;
  int tmp;
  l=monstrlen(src);
  for(i=l - 1;i >=0;i--)
    {
      tmp=(int)src[i];
      ungetc(tmp,f);
    }
  free(src);
}
int monstrlen(char*v)
{
  int i;
  for(i=0;v[i] != '\0'; i++)
    {}
  if(i==0)
    return 0;

  return(i);
}
char* monstrcat(char*v,char*w)
{
  int i;
  i=monstrlen(v);
  if(i==0)
    {
      v[0]=w[0];
      v[1]='\0';
    }
  else
    {
      v[i]=w[0];
      v[i+1]='\0';
    }
  return v;

}
int estBooleen(char*dtc)
{
  return 1;
}
int accoladeVraiAig(FILE*f)
{
  int cmpt3;
  int cmp=0;
  cmpt3=fgetc(f);
  char*a=malloc(2*sizeof(char));
  char*src=malloc(256*sizeof(char));
  src[0]='\0';
  a[0]=(char)cmpt3;
  a[1]='\0';
  src=monstrcat(src,a);
  if(a[0]=='{')
    {
      cmp=1;
      while((debutDeFonction(f))||(!cmp))
        {
	  cmpt3=fgetc(f);
	  a[0]=(char)cmpt3;
	  src=monstrcat(src,a);
	  if(!strcmp (src, "cas"))
            {
	      if(a[0]=='{')
                {
		  cmp++;
                }
	      else if(a[0]=='}')
                {
		  cmp--;
                }
            }
	  else
            {
	      return 0;
            }
        }
    }
  return cmp;
}

int pourParentheseVrai(FILE*f)
{
  int cmpt3;
  int cmp=0;
  cmpt3=fgetc(f);
  char*a=malloc(2*sizeof(char));
  char*str=malloc(256*sizeof(char));
  str[0]='\0';
  a[0]=(char)cmpt3;
  a[1]='\0';
  str=monstrcat(str,a);
  char*srcg=malloc(512*sizeof(char));
  srcg[0]='\0';
  srcg=monstrcat(srcg,a);
  if(a[0]=='(')
    {
      cmp++;
      while(a[0]!='{')
        {
	  cmpt3=fgetc(f);
	  a[0]=(char)cmpt3;
	  monstrcat(str,a);
	  srcg=monstrcat(srcg,a);
	  if(a[0]=='(')
	    cmp++;
	  if(a[0]==')')
	    cmp--;
	  pointVirguleVrai(f);
        }
      if(cmp==0)
        {
	  srcg=monstrcat(srcg,a);
	  return 1;
        }
      if(cmp!=0)
        {
	  srcg=monstrcat(srcg,a);
	  return 0;
        }
    }
  else
    srcg=monstrcat(srcg,a);return 0;
}
int verifProtFonction(FILE*f)
{
  char*a=malloc(2*sizeof(char));
  char*src=malloc(256*sizeof(char));
  src[0]='\0';
  int cmpt3=fgetc(f);
  a[0]=(char)cmpt3;
  a[1]='\0';
  src=monstrcat(src,a);
  char*srcg=malloc(512*sizeof(char));
  srcg[0]='\0';
  srcg=monstrcat(srcg,a);
  while(a[0]!=(')'))
    {
      while(a[0]!=(')')||a[0]!=(','))
        {
	  cmpt3=fgetc(f);
	  a[0]=(char)cmpt3;
	  src=monstrcat(src,a);
	  srcg=monstrcat(srcg,a);
	  if((!strcmp (src, "ent"))||(!strcmp (src, "flott"))||(!strcmp (src, "cour"))||(!strcmp (src, "vide"))||(!strcmp (src, "longue"))||(!strcmp (src, "gflott"))||(!strcmp (src, "car"))||((a[0]>='a')&&(a[0]<='z'))||((a[0]>='A')&&(a[0]<='Z'))||((a[0]>=0)&&(a[0]<=9))||(a[0]=' ')){}
	  else
            {
	      mongetc(f,srcg);return 0;
            }
        }
    }
  mongetc(f,srcg);return 1;
}
int debutDeFonction(FILE*f)
{
  int cmpt3;
  cmpt3=fgetc(f);
  char*a=malloc(2*sizeof(char));
  char*src=malloc(256*sizeof(char));
  char*srcg=malloc(512*sizeof(char));
  srcg[0]='\0';
  src[0]='\0';
  a[0]=(char)cmpt3;
  a[1]='\0';
  srcg=monstrcat(srcg,a);
  while((a[0]!='\n')||(a[0]!='\r'))
    {
      cmpt3=fgetc(f);
      a[0]=(char)cmpt3;
      src=monstrcat(src,a);
      srcg=monstrcat(srcg,a);
      if((!strcmp (src, "ent"))||(!strcmp (src, "flott"))||(!strcmp (src, "cour"))||(!strcmp (src, "vide"))||(!strcmp (src, "longue"))||(!strcmp (src, "gflott"))||(!strcmp (src, "car")))
        {
	  src[0]='\0';
	  while(a[0]!='(')
            {
	      srcg=monstrcat(srcg,a);
	      if(((a[0]>='a')&&(a[0]<='z'))||((a[0]>='A')&&(a[0]<='Z'))||((a[0]>=0)&&(a[0]<=9))||(a[0]=' '))
                {
		  if (verifProtFonction(f)==1)
                    {
		      mongetc(f,srcg);
                    }
		  else
                    {
		      mongetc(f,srcg);
		      return 0;
                    }
                }
	      else
		mongetc(f,srcg);return 0;
            }
        }
      else
	mongetc(f,srcg);return 0;
    }
  mongetc(f,srcg);return 1;
}
int lireLeFichier()
{
  FILE* f;
  int i=1;
  int*pi=&i;
  printf("Rentrez l'adresse du fichier: ");
  //scanf("%s",&a);
  f=fopen("toto.txt","r");
  if (f==NULL)
    {
      printf("\nUne erreur s'est produite lors de l'ouverture du fichier!");
      return 0;
    }
  if(parser2(f,pi)==0)
    {
      printf("\nUne erreur s'est produite a la ligne %d, veuillez la corriger\n",i);
      return 0;
    }
  if (fclose(f)!=0)
    {
      printf("\nErreur de la fermeture du fichier");
      return 0;
    }
  printf("\nit's good\n");
  return 1;
}
int parentheseVrai(FILE*f)
{
  int cmpt3;
  int cmp=0;
  cmpt3=fgetc(f);
  char*a=malloc(2*sizeof(char));
  char*src=malloc(256*sizeof(char));
  src[0]='\0';
  a[0]=(char)cmpt3;
  a[1]='\0';
  src=monstrcat(src,a);
  char*srcg=malloc(512*sizeof(char));
  srcg[0]='\0';
  srcg=monstrcat(srcg,a);
  if(a[0]=='(')
    {
      cmp++;
      while(a[0]=='{')
        {
	  cmpt3=fgetc(f);
	  a[0]=(char)cmpt3;
	  src=monstrcat(src,a);
	  srcg=monstrcat(srcg,a);
	  if(a[0]=='(')
	    cmp++;
	  if(a[0]==')')
	    cmp--;
        }
      if(cmp==0)
        {
	  if(estBooleen(src)==1)
            {
	      mongetc(f,srcg);return 1;
            }
	  else
            {
	      mongetc(f,srcg);return 0;
            }
        }
      if(cmp!=0)
	mongetc(f,srcg);return 0;
      mongetc(f,srcg);return 42;
    }
  else
    mongetc(f,srcg);return 0;
}
int pointVirguleVrai(FILE*f)
{
  int cmpt3,j;
  cmpt3=fgetc(f);
  char*a=malloc(2*sizeof(char));
  a[0]=(char)cmpt3;
  a[1]='\0';
  int ptv=0;
  char*str2=malloc(256*sizeof(char));
  str2[0]='\0';
  int cmp=0;
  printf("\n");
  char*srcg=malloc(512*sizeof(char));
  srcg[0]='\0';
  srcg=monstrcat(srcg,a);
  if(a[0]=='(')
    {
      cmp++;
      cmpt3=fgetc(f);
      a[0]=(char)cmpt3;
      srcg=monstrcat(srcg,a);
      while((cmp!=0)&&(a[0]!='{'))
        {

	  cmpt3=fgetc(f);
	  a[0]=(char)cmpt3;
	  srcg=monstrcat(srcg,a);
	  if((ptv==0)&&(a[0]==';'))
	    ptv=1;
	  if((ptv)&&(a[0]==';'))
	    ptv=0;
	  if(ptv==1)
	    str2=monstrcat(str2,a);
	  if(a[0]=='(')
	    cmp++;
	  else if(a[0]==')')
	    cmp--;
        }
      if((ptv==0)&&(cmp==0))
        {
	  if(estBooleen(str2)==1)
            {
	      printf("coucou\n");
	      j=accoladeVrai(f);
	      mongetc(f,srcg);
	      return (j);
            }
	  else
            {
	      mongetc(f,srcg);
	      return 0;
            }
        }
      else
	mongetc(f,srcg);return 0;
    }
  mongetc(f,srcg);return 0;
}
FILE* supprimeEspace(FILE*f,int*pi)
{
  int cmpt3;
  char*a=malloc(2*sizeof(char));
  cmpt3=fgetc(f);
  a[0]=(char)cmpt3;
  while((a[0]==' ')||(a[0]=='\n')||(a[0]=='\r'))
    {

      if((a[0]=='\r')||(a[0]=='\n'))
        {
	  *pi=*pi+1;
	  printf("i : %d\n", *pi);
        }
      cmpt3=fgetc(f);
      a[0]=(char)cmpt3;
    }
  ungetc(cmpt3,f);
  return f;
}
int accoladeVrai(FILE*f)
{
  int cmpt3;
  cmpt3=fgetc(f);
  char*a=malloc(2*sizeof(char));
  char*src=malloc(256*sizeof(char));
  src[0]='\0';
  a[0]=(char)cmpt3;
  a[1]='\0';
  src=monstrcat(src,a);
  int cmp=0;
  if(a[0]=='{')
    {
      cmp=1;
      while((!debutDeFonction(f))&&(cmp==1))
        {
	  printf("cmp : %d\n",cmp);
	  cmpt3=fgetc(f);
	  a[0]=(char)cmpt3;
	  printf("%c",a[0]);
	  src=monstrcat(src,a);
	  if(a[0]=='{')
            {
	      cmp++;
            }
	  else if(a[0]=='}')
            {
	      cmp--;
            }
        }
      printf("%d",cmp);
      return(!cmp);
    }
  return 0;
}
int parser(FILE*f,int*pi)
{
  char*s=malloc(256*sizeof(char));
  char*src2;
  int cmpt=0;
  char*a=malloc(2*sizeof(char));
  int tmp;
  char*src=malloc(256*sizeof(char));
  src[0] ='\0';
  s[0]='\0';
  while((tmp=fgetc(f))!=EOF)
    {
      f=supprimeEspace(f,pi);
      a[0]=(char)tmp;
      a[1]='\0';
      src=monstrcat(src,a);
      printf("\n%s",src);
      if(a[0]=='"')
        {
	  cmpt++;
        }
      if((cmpt%2))
	{}
      else
        {
	  if((a[0]=='#')||(a[0]==' ')||(a[0]==('{'))||(a[0]==('}'))||(a[0]=='(')||(a[0]==')')||(a[0]==';')||(a[0]=='!')||(a[0]=='=')||(a[0]=='<')||(a[0]=='>')||(a[0]=='*')||(a[0]=='\\')||(a[0]=='\r')||(a[0]=='\n')||((a[0]>='0')&&(a[0]<='9')))
            {
	      src[0]='\0';
            }
        }
      if(!strcmp (src, "si"))
        {
	  if(parentheseVrai(f)==0)
            {
	      return 0;
            }
	  else
            {
	      if(accoladeVrai(f)==0)
                {
		  return 0;
                }
	      else
                {
		  src[0] ='\0';
                }
            }

        }
      else if(!strcmp (src, "tq"))
        {
	  if(parentheseVrai(f)==0)
            {
	      return 0;
            }
	  else
            {
	      if(accoladeVrai(f)==0)
                {
		  return 0;
                }
	      else
                {
		  src[0] ='\0';
                }
            }
        }
      else if(!strcmp (src, "sinon"))
        {
	  if(a[0]=='{')
            {
	      ungetc(tmp,f);
	      accoladeVrai(f);
            }
	  else if(a[0]=='s')
            {
	      ungetc(tmp,f);
            }
	  else
            {
	      return 0;
            }
	  src[0] ='\0';
        }
      else if(!strcmp (src, "pour"))
        {
	  if(pointVirguleVrai(f)==0)
            {
	      return 0;
            }
	  else
            {
	      src[0] ='\0';
            }
        }
      else if(!strcmp (src, "ent"))
        {
	  printf("\n coucou");
	  src[0] ='\0';
        }
      else if(!strcmp (src, "car"))
        {
	  src[0] ='\0';
        }
      else if(!strcmp (src, "flott"))
        {
	  src[0] ='\0';
        }
      else if(!strcmp (src, "faire"))
        {
	  src[0] ='\0';
        }
      else if(!strcmp (src, "aig"))
        {
	  if(pourParentheseVrai(f)==0)
            {
	      return 0;
            }
	  else
            {
	      if(accoladeVrai(f)==0)
                {
		  return 0;
                }
	      else
                {
		  src[0] ='\0';
                }
            }
        }
      else if(!strcmp (src, "cas"))
        {
	  while(a[0]!=':')
            {
	      tmp=getc(f);
	      a[0]=(char)tmp;
	      a[1]='\0';
	      if(((a[0]>=0)&&(a[0]<=9))||(a[0]=' ')){}
	      else
                {
		  return 0;
                }
            }
	  while((a[0]==' ')||(a[0]==';'))
            {
	      tmp=getc(f);
	      a[0]=(char)tmp;
	      a[1]='\0';
	      src2=monstrcat(src2,a);
            }
	  if(!strcmp (src2, "casse"))
            {
	      return 1;
            }
	  else
            {
	      return 0;
            }
	  src[0] ='\0';

        }
      else if(!strcmp (src, "cour"))
        {
	  src[0] ='\0';
        }
      else if(!strcmp (src, "vide"))
        {
	  src[0] ='\0';
        }
      else if(!strcmp (src, "longue"))
        {
	  src[0] ='\0';
        }
      else if(!strcmp (src, "gflott"))
        {
	  src[0] ='\0';
        }
      else if(!strcmp (src, "NULE"))
        {
	  src[0] ='\0';
        }
      else if(!strcmp (src, "ecrire"))
        {
	  while(a[0]!='(')
            {
	      tmp=getc(f);
	      a[0]=(char)tmp;
	      a[1]='\0';
	      if((a[0]==' ')||(a[0]=='(')){}
	      else
                {
		  return 0;
                }
            }
	  src[0] ='\0';


        }
      else if(!strcmp (src, "casse"))
        {
	  while(a[0]!=';')
            {
	      tmp=getc(f);
	      a[0]=(char)tmp;
	      a[1]='\0';
	      if((a[0]==';')||(a[0]==' ')){}
	      else
                {
		  return 0;
                }
            }
	  src[0] ='\0';
        }
      else if(!strcmp (src, "defaut"))
        {
	  while(a[0]!=':')
            {
	      tmp=getc(f);
	      a[0]=(char)tmp;
	      a[1]='\0';
	      if(a[0]==':'){}
	      else
                {
		  return 0;
                }
            }
	  src[0] ='\0';
        }
      else if(!strcmp (src, "definir"))
        {
	  src[0] ='\0';
        }
    }
  return 1;
}
int main()
{
  lireLeFichier();
  system("Pause");
  return 0;
}



